package com.example.zyh.toolslib.base;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseNewFragment extends Fragment {

    private boolean isLoad = true;
    protected Unbinder mUnbinder;

    protected LayoutInflater inflater;
    protected Activity mActivity;
    private View rootView;//缓存Fragment view

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
//        mUnbinder = ButterKnife.bind(this, view);
        if (rootView == null) {
            rootView = inflater.inflate(initLayout(), container, false);
        }
        // 缓存的viewiew需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个view已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        mUnbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initData();
           // load();
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        Log.e("-s-", getClass().getSimpleName()+" onResume");
        if (isVisible() && isLoad) {
            Log.e("lanjiazai","执行懒加载");
            isLoad = false;
            initData();
            //load();
        }else{
            initData();
            Log.e("buzhixing","不执行懒加载");
        }

    }

  /*  private void load() {
        Log.e("-s-", getClass().getSimpleName()+" 需要加载数据");
    }*/

    protected abstract void  initData();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }

    }

    protected abstract int initLayout();



}
